﻿using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class EditSourceFileUC : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title_FormUC1.setTitle(Request.QueryString[QueryString.file], ConstEx.funcButtonToolBar.VIEWFILE);
                MsgUC1.setClearMsg();
                if (!IsPostBack)
                {
                    //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                    //{
                    //    pnlMain.Visible = false;
                    //    Commons.setAccessDenied(Page, MsgUC1);
                    //    return;
                    //}
                    if (Request.QueryString[QueryString.file] != null)
                    {
                        string sFileContent = System.IO.File.ReadAllText(Server.MapPath("~/" + Request.QueryString[QueryString.file]));
                        txtFileContent.Text = sFileContent;
                    }
                    DanhSachFile();
                }
                txtFileContent.EnterMode = CKEditor.NET.EnterMode.BR;
                CKEditorEx.LoadCKEditor(txtFileContent);
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.INFO, MessagesEx.CHUC_NANG_NAY_DANG_TAM_KHOA);
                return;
                System.IO.File.WriteAllText(Server.MapPath("~/" + Request.QueryString[QueryString.file]), txtFileContent.Text);
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Lưu lại thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message);
            }
            //MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Tính năng này tạm khóa");
        }

        private void DanhSachFile()
        {
            string[] lstFileHtml = Directory.GetFiles(Server.MapPath("~/"), "*.html");
            string[] lstFileCss = Directory.GetFiles(Server.MapPath("~/App_Themes/"), "*.css");
            string[] lstFileJs = Directory.GetFiles(Server.MapPath("~/Scripts/"), "*.js");
            string[] lstFileAspx = Directory.GetFiles(Server.MapPath("~/"), "*.aspx");
            string[] lstFileTxt = Directory.GetFiles(Server.MapPath("~/App_Data/"), "*.txt");
            string[] lstFileXml = Directory.GetFiles(Server.MapPath("~/App_Data/"), "*.xml");
            string[] lstFileJson = Directory.GetFiles(Server.MapPath("~/App_Data/"), "*.json");
            string[] lstFileLog = Directory.GetFiles(System.Configuration.ConfigurationManager.AppSettings["RawUrl_RootPath"] + "GlobalResources\\Logs\\", "*.log");

            trvHTML.Nodes.Clear();
            foreach (string FileName in lstFileHtml)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode(trNood.Text);
                trvHTML.Nodes.Add(trNood);
            }

            trvCSS.Nodes.Clear();
            foreach (string FileName in lstFileCss)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("App_Themes/" + trNood.Text);
                trvCSS.Nodes.Add(trNood);
            }

            trvJS.Nodes.Clear();
            foreach (string FileName in lstFileJs)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("Scripts/" + trNood.Text);
                trvJS.Nodes.Add(trNood);
            }

            trvASPX.Nodes.Clear();
            foreach (string FileName in lstFileAspx)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode(trNood.Text);
                trvASPX.Nodes.Add(trNood);
            }

            trvTXT.Nodes.Clear();
            foreach (string FileName in lstFileTxt)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("App_Data/" + trNood.Text);
                trvTXT.Nodes.Add(trNood);
            }

            trvXML.Nodes.Clear();
            foreach (string FileName in lstFileXml)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("App_Data/" + trNood.Text);
                trvXML.Nodes.Add(trNood);
            }

            trvJSON.Nodes.Clear();
            foreach (string FileName in lstFileJson)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("App_Data/" + trNood.Text);
                trvJSON.Nodes.Add(trNood);
            }

            trvLOG.Nodes.Clear();
            foreach (string FileName in lstFileLog)
            {
                FileInfo fInfo = new FileInfo(FileName);
                TreeNode trNood = new TreeNode(fInfo.Name, fInfo.Name);
                trNood.NavigateUrl = FileNameByFunction.EditFile + HttpUtility.UrlEncode("GlobalResources/Logs/" + trNood.Text);
                trvLOG.Nodes.Add(trNood);
            }
        }
    }
}